//
//  PopoverAnimator.swift
//  PopViewMenu
//
//  Created by Rookie_YX on 16/5/27.
//  Copyright © 2016年 YX_Rookic. All rights reserved.
//

import UIKit

//  定义常量保存通知名称
let YXRPopoverAnimatorWillShow = "YXRPopoverAnimatorWillShow"
let YXRPopoverAnimatorWillDismiss = "YXRPopoverAnimatorWillDismiss"

class PopoverAnimator: NSObject ,UIViewControllerTransitioningDelegate,
    UIViewControllerAnimatedTransitioning
{
    // 记录当前UIPresentationController是否展开
    var isPresent:Bool = false
    // 定义属性保存菜单大小
    var presentFrame = CGRectZero
    
    //  实现代理方法, 告诉系统负责转场动画
    //  UIPresentationController  ios8  负责转场动画的
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    /**
     告诉系统谁来负责Modal的展现动画
     
     - parameter presented:  被展现视图
     - parameter presenting: 发起的视图
     - parameter source:
     
     - returns: 谁来负责
     */
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        
        isPresent = true
        NSNotificationCenter.defaultCenter().postNotificationName(YXRPopoverAnimatorWillShow, object: self)
        return self
    }
    
    /**
     告诉系统谁来负责Modal的消失动画
     
     - parameter dismissed: 被关闭的视图
     
     - returns: 数来负责
     */
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = false
        NSNotificationCenter.defaultCenter().postNotificationName(YXRPopoverAnimatorWillDismiss, object: self)
        return self
    }
    
    // MARK:-
    // MARK:- UIViewControllerAnimatedTransitioning
    
    /**
     返回动画时长
     
     - parameter transitionContext: 上下文,里面保存动画需要的所以得参数
     
     - returns: 动画时长
     */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    /**
     告诉系统如何动画, 无论是展现还是消失都会调用这个方法
     
     - parameter transitionContext: 上下文, 里面保存了动画需要所以参数
     */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //  展开
        if isPresent {
            let  toView = transitionContext.viewForKey(UITransitionContextToViewKey)
            toView?.transform = CGAffineTransformMakeScale(1.0, 0.0)
            // 注意一定要将视图加到容器上
            transitionContext.containerView()?.addSubview(toView!)
            // 设置锚点
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                toView?.transform = CGAffineTransformIdentity
            }) { (_) in
                transitionContext.completeTransition(true)
            }
        }
        else{
            // 关闭
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                //  注意:由于CGfloat是不准确的,所以如果写0.0会没有动画
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
        }
    }
}
